package com.jcl.recruit.verfity.logic;

import com.jcl.core.utils.DateUtils;
import com.jcl.recruit.verfity.mapper.VerificationCodeMapper;
import com.jcl.recruit.verfity.model.VerificationCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName VerificationCodeLogic
 * @Description TODO
 * @Author yyliu
 * @Date 2018/9/18 9:37
 * @Version 1.0
 **/
@Component
public class VerificationCodeLogic implements VerificationCodeMapper {

    @Autowired
    private VerificationCodeMapper verificationCodeMapper;

    /* *
     * @Author yyliu
     * @Description
     * @Date 9:43 2018/9/18
     * @Param [receiver, code, type, minute]
     * @return com.example.model.VerificationCode
     **/
    public VerificationCode addVerificationCode(String receiver,
                                   String code,
                                   Integer type,
                                   int minute){
        // 先删除
        delete(receiver, type);

        VerificationCode verificationCode = new VerificationCode();
        verificationCode.setReceiver(receiver);
        verificationCode.setType(type);
        verificationCode.setCode(code);

        verificationCode.setAbateDate(DateUtils.addMinutes(new Date(), minute));

        verificationCodeMapper.add(verificationCode);

        return verificationCode;
    }

    @Override
    public VerificationCode findOneByType(String receiver, Integer type) {
        return verificationCodeMapper.findOneByType(receiver, type);
    }

    @Override
    public VerificationCode validateTime(String receiver, Integer type, Date abateDate) {
        return verificationCodeMapper.validateTime(receiver, type, abateDate);
    }

    @Override
    public VerificationCode validateCode(String receiver, Integer type, String code) {
        return verificationCodeMapper.validateCode(receiver, type, code);
    }

    @Override
    public int add(VerificationCode verificationCode) {
        return verificationCodeMapper.add(verificationCode);
    }

    @Override
    public int delete(String receiver, Integer type) {
        return verificationCodeMapper.delete(receiver, type);
    }
}
